package com.xbongbong.pro.statistics.enums;

import com.xbongbong.paas.constant.XbbConstant;
import lombok.Getter;

import java.util.Objects;

/**
 * BI用到的公式
 */
@Getter
public enum BiMathEnum {
    /** 精确计算和 **/
    ADD("math", "ADD","+"),
    /**函数使所有以参数形式给出的数字相加并返回和。*/
    SUM("math", "SUM","+"),
    /**返回参数的平均值（算术平均值）*/
    AVERAGE("math", "AVERAGE","+"),
    /** 精确计算差 **/
    SUBTRACT("math", "SUBTRACT","-"),
    /** 精确计算积 **/
    MULTIPLY("math", "MULTIPLY","*"),
    /** 精确计算商 **/
    DIVIDE("math", "DIVIDE","/"),
    /**返回一组值中的最大值。*/
    MAX("math", "MAX","greatest"),
    /**返回一组值中的最小值。*/
    MIN("math", "MIN","least"),
    /**
     * 获取两个日期字段的时间间隔
     */
    DATE_DIFF("math", "DATE_DIFF",","),
    /**
     * 获取当前的日期时间
     */
    NOW("math", "NOW", "NOW()"),
    /**
     *
     */
    IF("logic", "IF","if"),
    /**
     * 计数：只能包含一个字段，因此计算符号为""
     */
    COUNT("count", XbbConstant.COUNT,""),
    /**
     * 去重计数：只能包含一个字段，因此计算符号为""
     */
    COUNTD("count", XbbConstant.COUNTD,""),
        ;
    /**
     * 类型：
     */
    private String type;
    /**
     * 公式
     */
    private String formula;
    /**
     * 对应的CK sql数学符号
     */
    private String arithmetic;

    BiMathEnum(String type, String formula, String arithmetic) {
        this.type = type;
        this.formula = formula;
        this.arithmetic = arithmetic;
    }

    public static String getArithmeticByFormula(String formula) {
        for (BiMathEnum value : values()) {
            if(Objects.equals(formula,value.getFormula())){
                return value.getArithmetic();
            }  
        }
        return "";
    }

    /**
     * 嵌套一层，对DIVIDES做特殊处理
     *
     * @param formula
     * @return java.lang.String
     * @author zyy
     * @date 2022/9/20
     * @version v1.0
     */
    public static String getArithmetic4Formula(String formula) {
        if("DIVIDES".equals(formula)){
            return DIVIDE.getArithmetic();
        }
        return getArithmeticByFormula(formula);
    }

    public static BiMathEnum getBiMathEnum4Formula(String formula){
        if("DIVIDES".equals(formula)){
            return DIVIDE;
        }
        for (BiMathEnum biMathEnum : values()) {
            if(biMathEnum.formula.equals(formula)){
                return biMathEnum;
            }
        }
        return null;
    }
}
